package com.efounder.frame.tabbar;

import android.content.Context;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.graphics.drawable.Drawable;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class TabBarItemImmediate extends LinearLayout {
	
	private ImageView imageView;
	private TextView tabTextView;
	private CircleTextView indicatorTextView;
	
	private String text;
	private int textSize = 15;
	private int tabColor;
	
	private int indicatorNumber = 0;
	private int indicatorTextColor = Color.BLACK;
	private int indicatorBackgroundColor = Color.RED;
	private int indicatorTextSize = 11;
	
	//暂时写死
	private int imageWidth = 30;
	private int imageHeight = 30;
	
//	private int alpha;
	
	public TabBarItemImmediate(Context context, String text) {
		this(context, text, null);
	}
	
	public TabBarItemImmediate(Context context, String text, Drawable drawable) {
		super(context);
		this.text = text;
		
		setOrientation(VERTICAL);
		setGravity(Gravity.CENTER);
		
		int relativeLayoutWidth = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, imageWidth+4, getResources().getDisplayMetrics());
		int relativeLayoutHeight = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, imageHeight+2, getResources().getDisplayMetrics());
		
		//1 上部图片 + 指示器
		RelativeLayout relativeLayout = new RelativeLayout(context);
//		relativeLayout.setBackgroundColor(Color.BLUE);
		LinearLayout.LayoutParams relativeLayoutParams = new LinearLayout.LayoutParams(relativeLayoutWidth, relativeLayoutHeight);
		relativeLayout.setLayoutParams(relativeLayoutParams);
		//--1.1 图片
		int imageViewWidth = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, imageWidth, getResources().getDisplayMetrics());
		int imageViewHeight = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, imageHeight, getResources().getDisplayMetrics());
		imageView = new ImageView(context);
		if (drawable != null) {
			imageView.setImageDrawable(drawable);
		}
		RelativeLayout.LayoutParams imageViewParams = new RelativeLayout.LayoutParams(imageViewWidth, imageViewHeight);
		imageViewParams.addRule(RelativeLayout.CENTER_IN_PARENT);
		imageView.setLayoutParams(imageViewParams);
		relativeLayout.addView(imageView);
		//--1.2 指示器
		indicatorTextView = new CircleTextView(context);
//		final int indicatorWidth = (int)Math.ceil(imageViewWidth/2);
//		RelativeLayout.LayoutParams indicatorParams = new RelativeLayout.LayoutParams(indicatorWidth, indicatorWidth);
		RelativeLayout.LayoutParams indicatorParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		indicatorParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
		indicatorParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
		indicatorTextView.setLayoutParams(indicatorParams);
		relativeLayout.addView(indicatorTextView);
		indicatorTextView.setGravity(Gravity.CENTER);

		setIndicatorNumber(indicatorNumber);
		indicatorTextView.setTextColor(indicatorTextColor);
		indicatorTextView.setBackgroundColor(indicatorBackgroundColor);
		indicatorTextView.setTextSize(indicatorTextSize);

		addView(relativeLayout);
		//2 下部文字
		LinearLayout.LayoutParams textViewParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		tabTextView = new TextView(context);
		tabTextView.setLayoutParams(textViewParams);
		tabTextView.setText(text);
		tabTextView.setGravity(Gravity.CENTER);
		addView(tabTextView);
	}
	
	

	public int getIndicatorNumber() {
		return indicatorNumber;
	}

	/**
	 * 设置指示器数字
	 * @param indicatorNumber
	 */
	public void setIndicatorNumber(int indicatorNumber) {
		this.indicatorNumber = indicatorNumber;
		if (indicatorNumber <= 0) {
			clearIndicator();
		}else {
			indicatorTextView.setNotifiText(indicatorNumber); 
			indicatorTextView.setVisibility(View.VISIBLE);
		}
	}
	
	public void clearIndicator(){
		indicatorTextView.setVisibility(View.INVISIBLE);
		indicatorNumber = 0;
	}

	public int getIndicatorTextColor() {
		return indicatorTextColor;
	}

	public void setIndicatorTextColor(int indicatorTextColor) {
		this.indicatorTextColor = indicatorTextColor;
		indicatorTextView.setTextColor(indicatorTextColor);
	}

	public int getIndicatorBackgroundColor() {
		return indicatorBackgroundColor;
	}

	public void setIndicatorBackgroundColor(int indicatorBackgroundColor) {
		this.indicatorBackgroundColor = indicatorBackgroundColor;
		indicatorTextView.setBackgroundColor(indicatorBackgroundColor);
	}

	public int getIndicatorTextSize() {
		return indicatorTextSize;
	}

	public void setIndicatorTextSize(int indicatorTextSize) {
		this.indicatorTextSize = indicatorTextSize;
		indicatorTextView.setTextSize(indicatorTextSize);
	}

	public String getTabText() {
		return text;
	}

	public void setTabText(String text) {
		this.text = text;
		tabTextView.setText(text);
	}


	public int getTabTextSize() {
		return textSize;
	}

	protected void setTabTextSize(int textSize) {
		this.textSize = textSize;
		tabTextView.setTextSize(textSize);
	}

	public int getTabColor() {
		return tabColor;
	}

	protected void setTabColor(int tabColor) {
		this.tabColor = tabColor;
		
		changeColor(tabColor);
	}
	
	private void changeColor(int tabColor) {
		tabTextView.setTextColor(tabColor);
		PorterDuffColorFilter filter = new PorterDuffColorFilter(tabColor, PorterDuff.Mode.SRC_IN);
		imageView.setColorFilter(filter);
	}

//	public int getTabAlpha() {
//		return alpha;
//	}

//	public void setTabAlpha(float alphaF) {
////		alphaF = 0.3f;
//		Log.i("tab", "-----tabColor:" + tabColor);
//		this.alpha = (int) Math.ceil((0x000000FF * alphaF));
//		Log.i("tab", "-----alpha:" + Integer.toHexString(this.alpha));
//		Log.i("tab", "-----alpha:" + Integer.toHexString(this.alpha << (6 * 4)));
//		Log.i("tab", "-----alpha:" + Integer.toHexString(this.alpha << (6 * 4) | 0x00FFFFFF));
//		int color = tabColor & (this.alpha << (6 * 4) | 0x00FFFFFF);
//		Log.i("tab", "-----color:" + Integer.toHexString(color));
//		changeColor(color);
//	}
	
}
